package com.dly.hope.controller;

import com.dly.hope.annotation.LoginRole;
import com.dly.hope.constant.R;
import com.dly.hope.dto.WebSocketMessageDto;
import com.dly.hope.model.ro.ChatListRo;
import com.dly.hope.service.ChatService;
import com.dly.hope.vo.SessionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "聊天相关接口")
@RequestMapping(value = "/chat")
public class ChatController extends BaseController{


    @Resource
    private ChatService chatService;

    @GetMapping("/getSessionVo")
    @ApiOperation(value = "获取会话信息")
    @LoginRole
    public ResponseEntity<R<List<SessionVo>>> getSessionVo(){
        return operate(()->this.successDate(chatService.getSessionVo(getCurrentUserId())));
    }

    @PostMapping("/createSession")
    @ApiOperation(value = "创建会话")
    @LoginRole
    public ResponseEntity<R<SessionVo>> creatSession(ChatListRo chatListRo){
        return operate(()->this.successDate(chatService.creatSession(chatListRo, getCurrentUserId())));
    }

    @PostMapping("/deleteSession")
    @ApiOperation(value = "删除会话")
    @LoginRole
    public ResponseEntity<R<Void>> deleteSession(ChatListRo chatListRo){
        chatService.deleteSession(chatListRo, getCurrentUserId());
        return operate(this::success);
    }

    @GetMapping("/getChatList")
    @ApiOperation(value = "获取聊天记录")
    @LoginRole
    public ResponseEntity<R<List<WebSocketMessageDto>>> getChatList(ChatListRo chatListRo){
        return operate(()->this.successDate(chatService.getChatList(getCurrentUserId(),chatListRo)));
    }

    @GetMapping("/setRead")
    @ApiOperation(value = "设置为已读")
    @LoginRole
    public ResponseEntity<R<Void>> setRead(ChatListRo chatListRo){
        chatService.setRead(chatListRo, getCurrentUserId());
        return operate(this::success);
    }
}
